<?php
class M_student_payment_breakdown Extends MY_Model
{
	public function get_payment_breakdown_by_spr_id($id)
	{
		if(is_numeric($id))
		{
			$id = intval($id);
			
			$sql = "SELECT `spb`.`spb_id`,
							`spb`.`spb_fee_name`,
							`spb`.`spb_fee_rate`,
							`spb`.`spb_amount_paid`,
							`spb`.`spb_remaining_balance`,
							`spb`.`spb_is_tuition_fee`,
							`spb`.`spb_is_registration_fee`,
							`spb`.`spb_is_misc_fee`,
							`spr`.`spr_or_no`
					FROM student_payment_breakdown spb
					LEFT JOIN student_payment_records spr ON spb.spb_spr_id = spr.spr_id
					WHERE spb_spr_id = ? ORDER BY spb_is_tuition_fee DESC";
			$query = $this->db->query($sql,array($id));
			return $query->num_rows() >=1 ? $query->result() : FALSE;
		}else{
			return FALSE;
		}
	}
	
	public function get_payment_record_by_enrollment_id($id)
	{
		$id = intval($id);
		
		$sql = "SELECT `spr_id`,
						`spr_or_no`,
						`spr_payment_date`,
						`spr_ammt_paid`,
						`spr_created`
				FROM `student_payment_records` 
				WHERE `spr_enrollment_id` = ? 
				AND spr_schoolyear_id = ?
				AND spr_is_deleted = 0
				ORDER BY spr_payment_date ASC";
		$query = $this->db->query($sql,array($id,$this->c_sy->id));
		return $query->num_rows() >=1 ? $query->result() : FALSE;
	}
	
	public function get_specific_payment_breakdown_by_spr_id($id,$type = FALSE)
	{
		if(is_numeric($id) AND $type !== FALSE)
		{
			$id = intval($id);
			
			// $sql = "SELECT `spb`.`spb_id`,
							// `spb`.`spb_fee_name`,
							// `spb`.`spb_fee_rate`,
							// `spb`.`spb_amount_paid`,
							// `spb`.`spb_remaining_balance`,
							// `spb`.`spb_is_tuition_fee`,
							// `spb`.`spb_is_other_fee`,
							// `spb`.`spb_is_elective_fee`,
							// `spb`.`spb_is_cca_fee`,
							// `spb`.`spb_is_misc_fee`,
							// `spr`.`spr_or_no`
					// FROM student_payment_breakdown spb
					// LEFT JOIN student_payment_records spr ON spb.spb_spr_id = spr.spr_id
					// WHERE spb_spr_id = ?
					// AND spb_sy_id = ?
					// ORDER BY spb_is_tuition_fee,
					// spb_is_misc_fee ASC";
			
			switch(strtolower($type))
			{
				case $type == 'tuit':
					$this->db->where('spb_is_tuition_fee',1);
				break;
				case  $type == 'other':
					$this->db->where('spb_is_other_fee',1);
				break;
				case  $type == 'elec':
					$this->db->where('spb_is_elective_fee',1);
				break;
				case  $type == 'cca':
					$this->db->where('spb_is_cca_fee',1);
				break;
				case  $type == 'misc':
					$this->db->where('spb_is_misc_fee',1);
				break;
			}
			
			
			$this->db->select('`spb`.`spb_id`,
							`spb`.`spb_fee_name`,
							`spb`.`spb_fee_rate`,
							`spb`.`spb_amount_paid`,
							`spb`.`spb_remaining_balance`,
							`spb`.`spb_is_tuition_fee`,
							`spb`.`spb_is_other_fee`,
							`spb`.`spb_is_elective_fee`,
							`spb`.`spb_is_cca_fee`,
							`spb`.`spb_is_misc_fee`,
							`spr`.`spr_or_no`')
					 ->join('student_payment_records spr','spb.spb_spr_id = spr.spr_id','left')
					 ->where('spb_spr_id',$id)
					 ->where('spb_sy_id',$this->c_sy->id)
					 ->order_by('spb_is_tuition_fee,spb_is_misc_fee,spb_is_cca_fee ASC');
					 
					 
			$query = $this->db->get('student_payment_breakdown spb');
			return $query->num_rows() >=1 ? $query->result() : FALSE;
		}else{
			return FALSE;
		}
	}
}